package com.example.obs.service.DepartmentService;

import com.example.obs.dao.DepartmentMapper;
import com.example.obs.model.domin.Department;
import com.example.obs.model.domin.ObsFile;
import com.example.obs.service.FlileService.FileService;
import com.example.obs.service.ObjectService.BucketObjectService;
import com.example.obs.service.ObsService.ObsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Transactional
@Service

public class DepartmentServiceImpl implements DepartmentService{

    @Autowired
    DepartmentMapper departmentMapper;

    @Autowired
    ObsService obsService;

    @Autowired
    FileService fileService;

    @Autowired
    BucketObjectService bucketObjectService;

    @Override
    public List<Department> selectDepartment(Department department){

        List<Department> departments = departmentMapper.selectDepartmentsList(department);
        return departments;
    }

    @Override
    public int addDepartment(Department department){
        List<Department> departments = departmentMapper.selectDepartmentsList(department);
        if(departments.size()<1){
            try{
                departmentMapper.addDepartment(department);
                bucketObjectService.newFolder("部门/"+department.getDepartmentName());
                ObsFile obsFile = new ObsFile();
                obsFile.setFileState(1);
                obsFile.setUserId(0);
                obsFile.setFileName(department.getDepartmentName());
                obsFile.setPath("部门/"+department.getDepartmentName());
                obsFile.setType("文件夹");
                fileService.addFile(obsFile);
                return 1;
            }catch (Exception e){
                e.printStackTrace();
                return 0;
            }
        }
        else {
            return 2;
        }
    }

    @Override
    public void deleteDepartment (Integer id){
        departmentMapper.deleteByPrimaryKey(id);
        //删除该部门桶
        obsService.deleteBucket(id);
    }

    @Override
    public int updateName(Department department){
        List<Department> departments = departmentMapper.selectDepartmentsList(department);
        if (departments.size()<1) {
            departmentMapper.updatename(department);
            return 1;
        }
        else
            return 0;
    }

    @Override
    public List<Department> ListDepartment(){return  departmentMapper.selectDepartmentAndAdmin();}
}
